
import 'package:barcode_widget/barcode_widget.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yp/helper.dart';
import 'package:yp/router.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'logic.dart';
import 'state.dart';

class MembershipCardHomePage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _MembershipCardHomePage();
  }
}

class _MembershipCardHomePage extends State<MembershipCardHomePage> {
  final MembershipCardHomePageLogic logic = Get.put(MembershipCardHomePageLogic());
  final MembershipCardHomePageState state = Get.find<MembershipCardHomePageLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        backgroundColor: Color(0xffEA5504),
        title: Text(
          '会员卡主页',
          style: TextStyle(
            color: Colors.white,
            fontSize: 17,
          ),
        ),
      ),
      body: GetBuilder<MembershipCardHomePageLogic>(
        builder: (logic) {
          String phoneStr = state.userInformationData == null ? '' : state.userInformationData['mobile'];
          if (phoneStr.length == 11) {
            phoneStr = '${phoneStr.substring(0, 4)}****${phoneStr.substring(8, 11)}';
          }
          return Container(
            color: Color(0xff242020),
            child: SmartRefresher(
              controller: state.controller1,
              header: CustomHeader(
                builder: (BuildContext context, RefreshStatus mode) {
                  Widget body;
                  if (mode == RefreshStatus.completed) {
                    body = CupertinoActivityIndicator();
                  }
                  return Container(
                    height: 55.0,
                    child: Center(child: body),
                  );
                },
              ),
              enablePullDown: true,
              onRefresh: logic.refreshData,
              child: ListView(
                padding: EdgeInsets.only(
                  top: setWidth(20),
                ),
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: setWidth(20)),
                    child: Column(
                      children: [
                        Stack(
                          children: [
                            Image.asset(
                              'assets/images/crownBg_image.jpg',
                              fit: BoxFit.fill,
                            ),
                            Padding(
                              padding: EdgeInsets.only(
                                left: setWidth(70),
                                top: setWidth(88),
                                right: setWidth(70),
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Row(
                                    children: [
                                      ClipOval(
                                        child: CachedNetworkImage(
                                          imageUrl: state.userInformationData != null ? (state.userInformationData['logo'] ?? '') : '',
                                          width: setWidth(100),
                                          height: setWidth(100),
                                          errorWidget: (context, url, error) {
                                            return Container(
                                              child: ClipOval(
                                                child: Image.asset(
                                                  'assets/images/defaultAvatar_image.png',
                                                  width: setWidth(100),
                                                  height: setWidth(100),
                                                  fit: BoxFit.cover,
                                                ),
                                              ),
                                            );
                                          },
                                          fit: BoxFit.cover,
                                        ),
                                      ),
                                      SizedBox(
                                        width: setWidth(16),
                                      ),
                                      state.userInformationData == null ? Container() : Expanded(
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Row(
                                              children: [
                                                Text(
                                                  phoneStr,
                                                  style: TextStyle(
                                                    color: Color(0xff805526),
                                                    fontSize: 17,
                                                  ),
                                                ),
                                                SizedBox(width: setWidth(10),),
                                                Container(
                                                  height: setWidth(30),
                                                  padding: EdgeInsets.symmetric(horizontal: setWidth(16)),
                                                  decoration: BoxDecoration(
                                                    border: Border.all(
                                                      width: setWidth(1),
                                                      color: Color(0xff805526),
                                                    ),
                                                    borderRadius: BorderRadius.circular(setWidth(15)),
                                                  ),
                                                  alignment: Alignment.center,
                                                  child: Text(
                                                    state.userInformationData['vip_type_name'] ?? '',
                                                    style: TextStyle(
                                                      color: Color(0xff805526),
                                                      fontSize: 9,
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                            SizedBox(height: setWidth(6)),
                                            Row(
                                              children: [
                                                InkWell(
                                                  onTap: () {
                                                    print('！！！！  会员权益 按钮点击事件  ！！！！');
                                                    Get.toNamed(RouteConfig.membershipInterests);
                                                  },
                                                  child: Container(
                                                    height: setWidth(34),
                                                    padding: EdgeInsets.symmetric(horizontal: setWidth(16)),
                                                    decoration: BoxDecoration(
                                                      borderRadius: BorderRadius.circular(setWidth(17)),
                                                      color: Color(0xff805526),
                                                    ),
                                                    alignment: Alignment.center,
                                                    child: Text(
                                                      '会员权益>',
                                                      style: TextStyle(
                                                        color: Colors.white,
                                                        fontSize: 9,
                                                      ),
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ],
                                        ),
                                      ),
                                    ],
                                  ),
                                  SizedBox(height: setWidth(30)),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Row(
                                        crossAxisAlignment: CrossAxisAlignment.center,
                                        children: [
                                          Icon(
                                            Icons.check_circle_outline,
                                            size: setWidth(22),
                                            color: Color(0xff805526),
                                          ),
                                          Text(
                                            ' 会员专享价',
                                            style: TextStyle(
                                              color: Color(0xff805526),
                                              fontSize: 9,
                                            ),
                                          ),
                                        ],
                                      ),
                                      Row(
                                        crossAxisAlignment: CrossAxisAlignment.center,
                                        children: [
                                          Icon(
                                            Icons.check_circle_outline,
                                            size: setWidth(22),
                                            color: Color(0xff805526),
                                          ),
                                          Text(
                                            ' 累计积分',
                                            style: TextStyle(
                                              color: Color(0xff805526),
                                              fontSize: 9,
                                            ),
                                          ),
                                        ],
                                      ),
                                      Row(
                                        crossAxisAlignment: CrossAxisAlignment.center,
                                        children: [
                                          Icon(
                                            Icons.check_circle_outline,
                                            size: setWidth(22),
                                            color: Color(0xff805526),
                                          ),
                                          Text(
                                            ' 使用优惠券',
                                            style: TextStyle(
                                              color: Color(0xff805526),
                                              fontSize: 9,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                        Padding(
                          padding: EdgeInsets.symmetric(horizontal: setWidth(20)),
                          child: Container(
                            height: setWidth(360),
                            width: MediaQuery.of(context).size.width - setWidth(40),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(setWidth(20)),
                                bottomRight: Radius.circular(setWidth(20)),
                              ),
                            ),
                            padding: EdgeInsets.symmetric(horizontal: setWidth(20), vertical: setWidth(30)),
                            child: state.userInformationData != null ? Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                state.userInformationData['usr_no'] != null && state.userInformationData['usr_no'] != '' ? BarcodeWidget(
                                  data: state.userInformationData['usr_no'].toString(),
                                  barcode: Barcode.code128(),
                                  width: setWidth(440),
                                  height: setWidth(160),
                                  drawText: false,
                                ) : Container(),
                                SizedBox(height: setWidth(40),),
                                Text(
                                  '卡号：${state.userInformationData['usr_no'] ?? ''}',
                                  style: TextStyle(
                                    color: Color(0xff222222),
                                    fontSize: 12,
                                  ),
                                ),
                              ],
                            ) : Container(),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}